// pages/index/panda_village/panda_village.js
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    tabs: [{
      title: "全部",
      type: "",
      pageNumber: 1,
      list: []
    },{
      title: "文创社区",
      type: 1,
      pageNumber: 1,
      list: []
    },{
      title: "特色街区",
      type: 2,
      pageNumber: 1,
      list: []
    },{
      title: "熊猫星球",
      type: 3,
      pageNumber: 1,
      list: []
    },{
      title: "客家文化",
      type: 4,
      pageNumber: 1,
      list: []
    },{
      title: "熊猫科普",
      type: 5,
      pageNumber: 1,
      list: []
    }],
    isRefresh: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * tab页切换
   * @param e
   */
  onChangeTab: function (e) {
    this.data.tabIndex = e.detail.index
    if (this.data.tabs[this.data.tabIndex].list.length <= 0) {
      this.getData()
    }
  },

  onRefresh: function (e) {
    this.data.tabs[this.data.tabIndex].pageNumber = 1
    this.data.tabs[this.data.tabIndex].list = []
    this.setData({
      isRefresh: true,
      tabs: this.data.tabs
    })
    this.getData()
  },

  onLower: function (e) {
    this.data.tabs[this.data.tabIndex].pageNumber++
    this.getData()
  },

  onDetail: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "./detail/detail?id=" + id
    })
  },

  /**
   * 获取数据
   */
  getData: function () {
    const index = this.data.tabIndex
    Toast.loading({
      duration: 0,
      message: "加载中..."
    })
    http.get("essay/page",{
      area_code: app.globalData.areaCode,
      type: this.data.tabs[index].type,
      page_number: this.data.tabs[index].pageNumber,
      page_size: 10,
    }).then(res => {
      Toast.clear()
      if (res.data.code === http.REQUEST_OK) {
        let list = this.data.tabs[index].list
        if (res.data.data.list.length > 0) {
          res.data.data.list.forEach(item => {
            if (item.type == 1) {
              item.type_name = "文创社区"
            } else if (item.type == 2) {
              item.type_name = "特色街区"
            } else if (item.type == 3) {
              item.type_name = "熊猫星球"
            } else if (item.type == 4) {
              item.type_name = "客家文化"
            } else {
              item.type_name = "熊猫科普"
            }
            list.push(item)
          })
          this.data.tabs[index].list = list
        }else {
          if (this.data.tabs[index].pageNumber > 1) {
            this.data.tabs[index].pageNumber--
          }
          Toast.fail("没有更多数据了")
        }
        this.setData({
          tabs: this.data.tabs
        })
      }else {
        if (this.data.tabs[index].pageNumber > 1) {
          this.data.tabs[index].pageNumber--
        }
        Dialog.alert({
          title: "温馨提示",
          message: res.data.msg
        }).then(() => {

        })
      }
      this.setData({
        isRefresh: false
      })
    }, err => {
      Toast.clear()
      this.setData({
        isRefresh: false
      })
      if (this.data.tabs[index].pageNumber > 1) {
        this.data.tabs[index].pageNumber--
      }
      Dialog.alert({
        title: "获取数据失败",
        message: "网络异常，请稍后再试！"
      }).then(() => {

      })
      // Toast.fail("网络异常，请稍后再试")
    })
  }
})